/**
 * @author Marcelo Miranda Carneiro - mcarneiro@gmail.com
 */
;(function(scope, $){
	if(!$) return;

	$(function(){
		var id = (document.location.search.match(/pr=(.*)?(?:$|&)/) || []).pop(),
			holder = $('.wrapper:first'),
			message = $('#message'),
			clear = $('#callClear'),
			callExport = $('#callExport'),
			create = new ProjectCreator($('#callCreate'));

		scope.checklist = new Control(id, {
			table: $('.checklist'),
			percent: $('#percentHolder')
		});
		
		scope.checklist
			.bind('firstClick', function() {
				message.remove();
			})
			.bind('oneEmptyLine', function() {
				holder.append(message);
			});

		clear.click(function() {
			scope.checklist.clear();
			return false;
		});

		create.bind('onCreate', function(e, data){
			scope.checklist.createProject(data.id);
			document.location.href = document.location.href.split('?')[0] + '?pr='+data.id;
			return false;
		});

		callExport.click('export', function() {
			var response = $(document.createElement('textarea'))
				.addClass('fldExport')
				.keyup(function(e){
					if(e.keyCode == 27){
						$(this).remove();
					}
				})
				.val(scope.checklist.getContent());
			holder.append(response);
			return false;
		});

		scope.checklist.build();
	});
	

})(window, jQuery);